/**
 * Created by 马志 on 2016/2/23.
 */

/*
* 消息处理类
* */
/**
 * Created by 马志 on 2016/2/23.
 */

/*
 * 消息处理类
 * */
(function (exports) {
    function MessageHander() {
        this.callbacks = {onreceived_msg: null};
    }

    MessageHander.prototype.addMessageHander=function(message_hander){
        this.callbacks.onreceived_msg=message_hander;
        var self=this;
        chrome.runtime.onMessageExternal.addListener(
            function (request, sender, sendResponse) {
                if (self.callbacks.onreceived_msg){
                    var response=self.callbacks.onreceived_msg(request);
                    if (response){
                        sendResponse(response);
                    }
                }
            });
    }

    exports.MessageHander = MessageHander;
})(window);


///*
// * 创建消息处理类实例
// * */
//var messageHander=null;
//(function(){
//    messageHander=new MessageHander();
//})();
//
//messageHander.addMessageHander(function(message){
//    message_hander_func(message);
//});
//
///*
// * 消息处理函数
// * 在此添加自己的业务逻辑
// * 返回的数据将会发送到发送者
// * */
//function message_hander_func(message){
//    alert("receive message in hander");
//    console.log(message);
//    return {result:"received message"};
//}
